const CommonApi = require('../../../api/common.js');
const UserApi = require('../../../api/user.js');
const commonUtil = require('../../../utils/common.js')

Page({
  data: {
    formModel: {
      username: null,
      password: null,
    },
    optionsData: {
      loginType: null,
      registerId: null,
      redirect_uri: null,
      redirect_uri_type: null,
    }
  },
  onLoad(options) {
    console.log(options);
    if (options.registerId) {
      this.setData({
        ['optionsData.loginType']: options.loginType,
        ['optionsData.registerId']: options.registerId,
        ['optionsData.redirect_uri']: options.redirect_uri,
        ['optionsData.redirect_uri_type']: options.redirect_uri_type,
      })
    }
  },
  onReady() {},
  onShow() {},
  onHide() {},
  onUnload() {},
  handleInput(e){
    let target = e.currentTarget.dataset.item;
    let value = e.detail.value;
    if (target == 'username') {
      this.setData({['formModel.username']: value})
    } else if (target == 'password') {
      this.setData({['formModel.password']: value})
    }
  },
  // 查询登录用户详情
  getLoginUser(){
    UserApi.getLoginUser().then(userRes=>{
      if (userRes.retCode == "000000" && userRes.errCode == "000000") {
        // this.setData({phoneNumber: userRes.data.phoneNumber});
        wx.setStorageSync('userInfo', userRes.data)
        wx.removeStorageSync('loginInfo')
        commonUtil.getUserServiceState();

        if (this.data.optionsData.loginType == 'psw') {
          if (!userRes.data.setPassword || !userRes.data.activated) {
            wx.setStorageSync('resetInfo', {'action': 'set', phoneNumber: userRes.data.phoneNumber})
            wx.redirectTo({
              url: '/packageA/pages/forgetPsw/index?redirect_uri='+this.data.optionsData.redirect_uri+'&redirect_uri_type='+this.data.optionsData.redirect_uri_type,
            })
          } else {
            if (this.data.optionsData.redirect_uri_type == 'tab') {
              setTimeout(()=>{
                wx.switchTab({url: this.data.optionsData.redirect_uri})
              }, 200)
            } else if (this.data.optionsData.redirect_uri_type == 'link') {
              setTimeout(()=>{
                wx.redirectTo({url: this.data.optionsData.redirect_uri})
              }, 200)
            }
          }
        } else {
          if (this.data.optionsData.redirect_uri_type == 'tab') {
            wx.switchTab({url: this.data.optionsData.redirect_uri})
          } else if (this.data.optionsData.redirect_uri_type == 'link') {
            wx.redirectTo({url: this.data.optionsData.redirect_uri})
          }
        }
        wx.showToast({icon: 'success', title: '登录成功'})
      }
    })
  },
  confirmClick(){
    let that = this;
    console.log(this.data.formModel);
    if (!this.data.formModel.username) {
      wx.showToast({icon: 'none', title: '请输入手机号/用户名'});
    } else if (!this.data.formModel.password) {
      wx.showToast({icon: 'none', title: '请输入密码'});
    } else {
      CommonApi.xcxLoginAndBind({...this.data.formModel, registerId: this.data.optionsData.registerId}).then(res=>{
        if (res.retCode == "000000" && res.errCode == "000000") {
          let expires = new Date(new Date() * 1 + res.data.expiresIn * 1000)
          wx.setStorageSync('lastLoginInfo', {phoneNumber: this.data.formModel.username});
          wx.setStorageSync('token', res.data.accessToken);
          wx.setStorageSync('tokenExpire', expires) // 记录token有效期限(毫秒值)
          setTimeout(()=>{
            that.getLoginUser()
          }, 200)
        } else {
          wx.showToast({icon: 'error', title: res.errMsg})
        }
      })
    }
  },
  goBack(){
    wx.navigateBack({delta: 1});
  },
  
  onPullDownRefresh() {},
  onReachBottom() {},
  onShareAppMessage() {}
})